package com.hd123.data.check.config;

import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.enums.FieldStrategy;
import com.baomidou.mybatisplus.enums.IdType;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.parser.ISqlParser;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * myBatis配置
 *
 * Date: 2017/12/21
 * Time: 18:48
 *
 * @author zhangxin
 */
@Configuration
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {

    protected static final String SQL_SESSION_FACTORY_BEAN_NAME = "mybatisPlus";
    protected static final String XML_MAPPER_LOCATION = "classpath:mapper/*/*.xml";
    protected static final String ENTITY_LOCATION = "com.hd123.data.check.dao.*";

    @Autowired
    private DataSource dataSource;

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * mybatis-plus分页插件
     */
    @Bean(name = "paginationInterceptor")
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType(DBType.MYSQL.getDb());
        // 开启 PageHelper 的支持
        paginationInterceptor.setLocalPage(true);
        List<ISqlParser> sqlParserList = new ArrayList<>();
        paginationInterceptor.setSqlParserList(sqlParserList);
        return paginationInterceptor;
    }

    /**
     * 乐观锁插件
     */
    @Bean(name = "optimisticLockerInterceptor")
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        OptimisticLockerInterceptor optimisticLockerInterceptor = new OptimisticLockerInterceptor();
        return optimisticLockerInterceptor;
    }

//    @Bean(name = MyMetaObjectHandler.META_OBJECT_HANDLER_BEAN_NAME)
//    public MyMetaObjectHandler myMetaObjectHandler() {
//        MyMetaObjectHandler myMetaObjectHandler = new MyMetaObjectHandler();
//        return myMetaObjectHandler;
//    }

    /**
     * 初始化bean
     *
     * @param paginationInterceptor mybatis-plus分页插件
     *
     * @param optimisticLockerInterceptor 乐观锁插件
     *
     * @return  返回bean
     */
    @Bean(name = SQL_SESSION_FACTORY_BEAN_NAME)
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(PaginationInterceptor paginationInterceptor,
           OptimisticLockerInterceptor optimisticLockerInterceptor) {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage(ENTITY_LOCATION);
        bean.setPlugins(new Interceptor[]{paginationInterceptor, optimisticLockerInterceptor});

        GlobalConfiguration globalConfiguration = new GlobalConfiguration();
        globalConfiguration.setDbType(DBType.MYSQL.getDb());
        // 主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
        globalConfiguration.setIdType(IdType.UUID.getKey());
        // 字段策略 0:"忽略判断",1:"非 NULL 判断",2:"非空判断"
        globalConfiguration.setFieldStrategy(FieldStrategy.IGNORED.getKey());
        // 驼峰下划线转换
        globalConfiguration.setDbColumnUnderline(true);
        //刷新mapper 调试神器
        globalConfiguration.setRefresh(true);
        //数据库大写下划线转换
        globalConfiguration.setCapitalMode(true);
        //逻辑删除配置
        globalConfiguration.setLogicDeleteValue(Integer.valueOf(1).toString());
        globalConfiguration.setLogicNotDeleteValue(Integer.valueOf(0).toString());
        globalConfiguration.setMetaObjectHandler(new MyMetaObjectHandler());
        bean.setGlobalConfig(globalConfiguration);
        try {
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(XML_MAPPER_LOCATION));
            return bean;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
